#!/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = "tingfeng"
# Date: 2020/4/27

import os
import pymysql
import traceback
import sys
import re
from appium import webdriver
import multiprocessing
import os
import time
import unittest
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC,\
    expected_conditions
import time
import unittest
from selenium.webdriver.common.by import By
import logging

from util.write_user_command import WriteUserCommand
from util.server import  Server
class BaseDriver():
    def android_driver(self,i):

        apk_path = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))  # 获取当前项目的根路径
        a = str(i)
        print(a)
        write_file = WriteUserCommand()
        devices = write_file.get_value('user_info_' + a, 'deviceName')
        port = write_file.get_value('user_info_' + a, 'port')

        C_caps = {}
        C_caps['platformName'] = 'Android'
        #C_caps['platformVersion'] = '9'
        C_caps['deviceName'] = devices  # 启动哪种设备，真机还是模拟器
        C_caps['app'] = apk_path + "\\apk\\youbei-test.apk"
        #C_caps["appPackage"] = "com.youbei.chefu"
        #C_caps["appActivity"] = "com.youbei.chefu.MainActivity"
        C_caps["noReset"] = True
        #C_caps["unicodeKeyboard"] = True  # 使用Unicode编码方式发送字符串
        #C_caps["resetKeyboard"] = True  # 隐藏键盘
        C_caps["automationName"] = "uiautomator2"
        print(C_caps)

        self.C_driver = webdriver.Remote("http://127.0.0.1:"+ port +"/wd/hub", C_caps)
        time.sleep(10)
        print(C_caps)

        print(apk_path)

        # time.sleep(3)
        # self.C_driver.find_element_by_xpath('//android.widget.TextView[@text="允许"]').click()
        # time.sleep(1)
        # self.C_driver.find_element_by_xpath('//android.widget.TextView[@text="允许"]').click()
        # time.sleep(1)
        # self.C_driver.find_element_by_xpath('//android.widget.TextView[@text="始终允许"]').click()
        # time.sleep(1)
        # self.C_driver.find_element_by_xpath('//android.widget.TextView[@text="允许"]').click()


if __name__ == "__main__":

    server = Server()
    server.kill_server()
    base_driver = BaseDriver()
    base_driver.android_driver()
    server.kill_server()
    # time.sleep(3)
    # base_driver.C_driver.find_element_by_xpath('//android.widget.TextView[@text="允许"]').click()
    # time.sleep(1)
    # base_driver.C_driver.find_element_by_xpath('//android.widget.TextView[@text="允许"]').click()
    # time.sleep(1)
    # base_driver.C_driver.find_element_by_xpath('//android.widget.TextView[@text="始终允许"]').click()
    # time.sleep(1)
    # base_driver.C_driver.find_element_by_xpath('//android.widget.TextView[@text="允许"]').click()





        # if i == 1:
        #     print("安卓门店版登录信息")
        #
        #     caps = {}
        #     caps["platformName"] = "Android"
        #     caps["platformVersion"] = "9"
        #     caps["deviceName"] = "HUAWEI Mate 10"
        #     #caps["app"] = appurl
        #     caps["appPackage"] = "com.youbei.store"
        #     caps["appActivity"] = "com.youbei.store.MainActivity"
        #     caps["noReset"] = True
        #     caps["unicodeKeyboard"] = True   # 使用Unicode编码方式发送字符串
        #     caps["resetKeyboard"] = True     #隐藏键盘
        #     caps["automationName"] = "uiautomator2"
        #     print(caps)
        #
        #     self.store_driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
        #     time.sleep(10)
        #     print(caps)
        #     return self.store_driver
        #
        # elif i ==2:
        #     print('安卓C端登录信息')
        #
        #     C_caps = {}
        #     C_caps["platformName"] = "Android"
        #     C_caps["platformVersion"] = "9"
        #     C_caps["deviceName"] = "HUAWEI Mate 10"
        #     # C_caps["app"] = appurl
        #     C_caps["appPackage"] = "com.youbei.chefu"
        #     C_caps["appActivity"] = "com.youbei.chefu.MainActivity"
        #     C_caps["noReset"] = True
        #     C_caps["unicodeKeyboard"] = True  # 使用Unicode编码方式发送字符串
        #     C_caps["resetKeyboard"] = True  # 隐藏键盘
        #     C_caps["automationName"] = "uiautomator2"
        #     print(C_caps)
        #
        #     self.c_driver = webdriver.Remote("http://localhost:4723/wd/hub", C_caps)
        #     time.sleep(10)
        #     print(C_caps)
        #     return self.c_driver
        #
        # else:
        #     print('艾斯比')
